<template>
    <el-dialog title="创建物资信息" :visible.sync="dialogFormVisible" width="35%" :append-to-body="true"
    @close="dialogClose">
      <el-form status-icon label-width="100px">
          <el-form-item label="仓库编号">
              <el-input v-model="warehouseinfo.fwhid" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="仓库名称">
              <el-input v-model="warehouseinfo.fwhname" type="text" autocomplete="off" />
          </el-form-item>
          <el-form-item label="仓库管理员">
            <el-select v-model="warehouseinfo.fwhleader" placeholder="请选择">
                <el-option
                  v-for="item in empList"
                  :key="item.feid"
                  :label="item.fename"
                  :value="item.feid">
                </el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="仓库地址">
            <el-input v-model="warehouseinfo.fwhaddr" type="textarea" autocomplete="off" />
          </el-form-item>
          <el-form-item label="备注">
              <el-input v-model="warehouseinfo.fwhmemo" type="textarea" placeholder="请输入内容" maxlength="200"
              show-word-limit  size="medium" autocomplete="off" />
          </el-form-item>
          <el-form-item>
              <el-button type="primary" @click="submitForm(warehouseinfo)">提交</el-button>
              <el-button @click="resetForm">重置</el-button>
          </el-form-item>
      </el-form>
    </el-dialog>
</template>
<script>
import { createWarehouse,updateWarehouse } from '@/api/warehouse'
import { getEmpList } from '@/api/emp'

export default {
    props: {
        dialogFormVisible: Boolean,
        editWarehouseInfo: {
            type:Array,
            default: []
        },
        updateOrCreate: Boolean
    },
    data() {
        return {
            warehouseinfo: {},
            empList: {}
        }
    },
    created () {
        this.initEmpList()  
    },
    updated () {
        this.warehouseinfo = this.editWarehouseInfo
    },
    methods: {
        initEmpList(){
            getEmpList().then(response => {
                this.empList = response.data.empList
            })
        },
        submitForm(warehouseinfo) {
            if (this.updateOrCreate) {
                updateWarehouse(warehouseinfo).then(response => {
                    this.$message.success('修改仓库信息成功')
                    this.$parent.fetchData()
                })
                this.dialogClose()
            } else {
                createWarehouse(warehouseinfo).then(response => {
                    this.$message.success('创建物资信息成功')
                    this.$parent.fetchData()     
                })
                this.dialogClose()
            }
        },
        resetForm() {
            this.warehouseinfo = {}
        },
        // 解决父页面按钮只能点击一次问题
        dialogClose() {
            this.dialogFormVisible = false
            this.$parent.vueReset()
        }
    }
}
</script>
    